package com.qf.service.impl;

import com.qf.dao.OrderDao;
import com.qf.pojo.Order;
import com.qf.pojo.OrderDetail;
import com.qf.pojo.resp.BaseResult;
import com.qf.service.OrderService;
import com.qf.utils.GenerateOrderNum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Package: com.qf.service.impl
 * @ClassName: OrderServiceImpl
 * @Author: lmr
 * @CreateTime: 2021/9/11 17:40
 * @Description:
 * @Version: 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;

    /**
     * 添加商品订单
     *
     * @param order
     * @return
     */
    @Override
    public BaseResult addOrder(Order order) {
        try {
            //设置订单号
            GenerateOrderNum generateOrderNum = new GenerateOrderNum();
            String s = generateOrderNum.generate("s");
            order.setOid(s);
            //设置订单时间
            order.setCreatTime(new Date());
            //设置订单状态
            order.setOrderStatus(0);
            orderDao.addOrder(order);
            System.out.println(order);
            return new BaseResult(1, "添加订单成功!", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResult(0, "添加订单失败!", null);
        }
    }

    /**
     * 根据用户id查询订单
     *
     * @param uid
     * @return
     */
    @Override
    public BaseResult findOrderByUid(Integer uid) {
        try {
            //查询用户下的所有订单
            List<Order> orderList = orderDao.findOrderByUid(uid);
            //System.out.println(orderList);
            List<OrderDetail> orderDetailList = new ArrayList<>();
            for (Order order : orderList) {
                OrderDetail orderDetail = orderDao.findOrderDetail(order);
                orderDetailList.add(orderDetail);
            }
            return new BaseResult(1,"订单查询成功!",orderDetailList);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResult(1,"订单查询失败!",null);
        }
    }

    /**
     * 根据商品id删除订单
     * @param oids
     * @return
     */
    @Override
    public BaseResult delOrderById(List<String> oids) {
        try {
            if (oids.size()>0){
                for (String oid : oids) {
                    orderDao.delOrderById(oid);
                }
                return new BaseResult(1,"订单删除成功!",null);
            }
            return new BaseResult(0,"订单删除失败!",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResult(0,"订单删除失败!",null);
        }
    }

    /**
     * 根据id查询订单
     * @param oidList
     * @return
     */
    @Override
    public List<OrderDetail> findOrderDetail(List<String> oidList) {
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (String oid : oidList) {
            OrderDetail orderDetail = orderDao.findOrderById(oid);
            if (orderDetail!=null) {
                orderDetailList.add(orderDetail);
            }
        }
        return orderDetailList;
    }

    /**
     * 支付完成后修改订单状态
     * @param oid
     * @return
     */
    @Override
    public Integer update(String oid) {

        return orderDao.update(oid);
    }

}
